package com.sinheldrin.musicplayer.main
{
	import com.sinheldrin.musicplayer.fields.Field;
	import com.sinheldrin.musicplayer.fields.FieldsController;
	import com.sinheldrin.musicplayer.library.LibraryController;
	import com.sinheldrin.musicplayer.library.LibraryView;
	import com.sinheldrin.musicplayer.music.Music;
	import com.sinheldrin.musicplayer.player.PlayerController;
	import com.sinheldrin.musicplayer.settings.SettingsController;
	
	import flash.events.Event;

	public class MainController
	{
		public function MainController(mainView:MainView)
		{
			_view = mainView;
			
			_settings = new SettingsController();
			_player = new PlayerController(view.playerView);
			_library = new LibraryController();
		}
		
		public function init():void
		{
			_settings.load();
			_settings.apply(view.application);
			
			_library.load();
			
			view.application.addEventListener(Event.CLOSING, onApplicationClosing);
			
			_player.init();
		}
		
		public function onApplicationClosing(closingEvent:Event):void
		{
			_library.save();
			
			_settings.retrieve(view.application);
			_settings.save();
		}
		
		private var _view:MainView = null;
		public function get view():MainView
		{
			return _view;
		}
		
		private var _settings:SettingsController = null;
		private var _player:PlayerController = null;
		
		private var _library:LibraryController = null;
		
		public function readLibrary():void
		{
			_player.setPlaylist(_library.toPlaylist());
		}
		
		public function saveLibrary():void
		{
			_library.save();
		}
		
		public function openLibrary():void
		{
			var libView:LibraryView = new LibraryView();
			libView.init(_library);
			libView.open();
		}
	}
}